import tkinter as tk
import tkinter.messagebox
from tkinter.constants import END

from class_1_sqlite import DataBase


class Page_sign_up:  # 注册页面
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("sign_up")
        self.window.geometry("500x300")
        self.window.config(bg="#0F375A")
        self.lf1()
        self.lf2()
        self.db = DataBase()
        self.window.mainloop()

    def lf1(self):
        lf = tk.LabelFrame(self.window)
        lf.place(x=20, y=20)

        lb1 = tk.Label(lf, text="用户名:")
        lb1.grid(row=1, column=1)

        entry_username = tk.Entry(lf, width=30)
        entry_username.grid(row=1, column=2)

        b1 = tk.Button(lf, text="检查用户名是否被占用", command=self.check_username)
        b1.grid(row=1, column=3)

        lb2 = tk.Label(lf, text="密码:")
        lb2.grid(row=2, column=1)

        entry_password = tk.Entry(lf, width=30)
        entry_password.grid(row=2, column=2)

        self.entry_username = entry_username
        self.entry_password = entry_password

    def lf2(self):
        lf = tk.LabelFrame(self.window)
        lf.place(x=20, y=100)

        b1 = tk.Button(lf, text="提交", command=self.submit)
        b1.grid(row=1, column=1)

        b2 = tk.Button(lf, text="重置", command=self.reset)
        b2.grid(row=1, column=2)

        b3 = tk.Button(lf, text="返回", command=self.turn_back_sign_in)
        b3.grid(row=1, column=3)

    def check_username(self):  # 检查按钮
        self.db.creat_if()
        if self.db.select(self.entry_username.get()) == None:  # 判断用户是否存在
            tkinter.messagebox.showinfo(title="Tips", message="检查按钮：此用户名未被注册")
        else:
            tkinter.messagebox.showinfo(title="Tips", message="检查按钮：用户名重复！")

    def reset(self):  # 重置按钮
        self.entry_username.delete(0, END)
        self.entry_password.delete(0, END)

    def turn_back_sign_in(self):  # 返回按钮
        self.window.destroy()
        # Page_sign_in() #此按钮，在本文件中不能使用。在终稿中可用。

    def submit(self):  # 提交按钮
        self.db.creat_if()
        username = self.entry_username.get()
        password = self.entry_password.get()

        if self.db.select(username) == None:  # 判断用户是否存在
            self.db.insert(username, password)
            print("注册成功！")
            tkinter.messagebox.showinfo(title="Tips", message="注册成功！")
        else:
            tkinter.messagebox.showinfo(title="Tips", message="提交结点，提示用户名重复！")
            print("提交结点，提示用户名重复！")


if __name__ == "__main__":
    Page_sign_up()
